<template>
  <AssetAssignList
    ref="refAssetList"
    :tag="'角色'"
    :name="props.entry.name"
    :assign-columns="assignColumns"
    :columns="columns"
    :load-assets="loadAssets"
    :assign-assets="assignAssets"
    :load-assign-assets="loadAssignAssets"
    :level-mode="true"
    :product="props.product"
  >
    <template v-slot:comment>
      <slot name="comment" />
    </template>
  </AssetAssignList>
</template>

<script setup lang="ts">
import {ref, getCurrentInstance} from 'vue'
import {useLoadAssign} from '@/utils/sop/UseLoadAssign'
import AssetAssignList from '@/views/sop/assign/AssetAssignList.vue'
import {currentNames, TYPES} from "@/stores/conf";
import {refreshUserAssets} from "@/utils/sop/RoleUtil";

const refAssetList = ref();

const props = withDefaults(
  defineProps<{
    entry: any,
    product?: boolean,
  }>(),
  {}
)

const {proxy} = getCurrentInstance();
const { load, assign, loadAssign, removeHolderOfAsset} = useLoadAssign(props.entry);

const productCode = () => {
  return refAssetList?.value.getProductCode();
}

const assignColumns = ref([
  {
    id: 'id',
    name: '标识',
    hide: 'true'
  },
  {
    id: 'name',
    name: '角色名称'
  },
  {
    id: 'code',
    name: '角色码'
  },
  {
    id: 'path',
    name: '路径'
  },
  {
    type: 'icon'
  }
])

const refreshUserMenus = async () => {
  if (await refreshUserAssets(currentNames.PLAT_USER)) {
    proxy.$bus.emit('roleChanged');
  }
}

const columns = ref([
  {
    id: 'id',
    name: '标识',
    hideCol: true,
    show: false
  },
  {
    id: 'name',
    name: '角色名称',
    show: true
  },
  {
    id: 'code',
    name: '角色码',
    show: true
  },
  {
    id: 'path',
    name: '路径',
    show: true
  },
  {
    id: 'operation',
    name: '操作',
    show: true,
    canEdit: false,
    hideCol: true,
    fixed: 'right',
    operations: [
      {
        type: 'confirm',
        name: '移除',
        disabled: (row: any) => {
          return row.noAuth == 1;
        },
        tips: () => { return '确认移除角色？' },
        confirm: async (data: any, row: any) => {
          await removeHolderOfAsset(getTargetId(), TYPES.ROLE, props.entry, row);
          setTimeout(async () => {
            await refreshUserMenus();
            refresh();
          },300);
        }
      }
    ]
  }
])

const getTargetId = () => {
  return '';
}

/**
 * 分页读取分配给当前角色的权限列表
 */
const loadAssets = async (params: any) => {
  if (params) {
    params.productCode = productCode();
  }
  return await load(getTargetId(), TYPES.ROLE, params)
}

/**
 * 为角色分配权限
 */
const assignAssets = async (items: any) => {
  const result: any = await assign(getTargetId(), TYPES.ROLE, items, productCode());
  setTimeout(() => refreshUserMenus(),300);
  return result;
}

/**
 * 分页读取可以分配给角色的权限列表
 */
const loadAssignAssets = async (params: any) => {
  return await loadAssign(getTargetId(), TYPES.ROLE, {
    ...params,
    productCode: productCode(),
  })
}

const refresh = () => {
  refAssetList.value?.refresh();
}

defineExpose({
  refresh,
})

</script>

<style scoped>
.el-form-item-class .el-form-item__label {
  font-size: 12px;
}

.role-assign-btn {
  margin: 16px 0;
}
</style>
